<template>
  <div class="login-container">
    <div class="login-form">
      <div class="login-logo">
        <img src="/api/placeholder/80/80" alt="Logo" class="logo-img" />
      </div>
      <h2 class="login-title">测盟汇管理系统</h2>

      <!-- 登录表单 -->
      <div v-if="!showRegister">
        <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" @submit.prevent="handleLogin">
          <el-form-item prop="userType">
            <el-select v-model="loginForm.userType" placeholder="请选择账号类型" style="width: 100%">
              <el-option label="企业用户" value="enterprise" />
              <el-option label="超级管理员" value="admin" />
            </el-select>
          </el-form-item>

          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名或手机号" :prefix-icon="User" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" :prefix-icon="Lock"
              show-password />
          </el-form-item>

          <el-form-item v-if="loginForm.userType === 'enterprise'" prop="captcha">
            <div class="captcha-row">
              <el-input v-model="loginForm.captcha" placeholder="请输入验证码" style="flex: 1;" />
              <div class="captcha-img" @click="refreshCaptcha">
                <img src="/api/placeholder/100/40" alt="验证码" />
              </div>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleLogin" :loading="loading" style="width: 100%">
              登录
            </el-button>
          </el-form-item>
        </el-form>

        <div class="register-link">
          <el-link type="primary" @click="switchToRegister">企业注册</el-link>
          <span style="margin: 0 10px;">|</span>
          <el-link type="primary" @click="openForgotPassword">忘记密码</el-link>
        </div>
      </div>

      <!-- 注册表单 -->
      <div v-if="showRegister">
        <div class="form-header">
          <h3>企业注册</h3>
          <el-button type="text" @click="switchToLogin">返回登录</el-button>
        </div>

        <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" @submit.prevent="handleRegister">
          <el-form-item prop="companyName">
            <el-input v-model="registerForm.companyName" placeholder="请输入企业名称" :prefix-icon="OfficeBuilding" />
          </el-form-item>

          <el-form-item prop="companyContact">
            <el-input v-model="registerForm.companyContact" placeholder="请输入企业联系方式" :prefix-icon="Phone" />
          </el-form-item>

          <el-form-item prop="username">
            <el-input v-model="registerForm.username" placeholder="请输入用户名" :prefix-icon="User" />
          </el-form-item>

          <el-form-item prop="email">
            <el-input v-model="registerForm.email" placeholder="请输入邮箱" :prefix-icon="Message" />
          </el-form-item>

          <el-form-item prop="phone">
            <el-input v-model="registerForm.phone" placeholder="请输入手机号" :prefix-icon="Phone" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" :prefix-icon="Lock"
              show-password />
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码" :prefix-icon="Lock"
              show-password />
          </el-form-item>

          <el-form-item prop="captcha">
            <div class="captcha-row">
              <el-input v-model="registerForm.captcha" placeholder="请输入验证码" style="flex: 1;" />
              <div class="captcha-img" @click="refreshCaptcha">
                <img src="/api/placeholder/100/40" alt="验证码" />
              </div>
            </div>
          </el-form-item>

          <el-form-item prop="agreement">
            <el-checkbox v-model="registerForm.agreement">
              我已阅读并同意
              <el-link type="primary" @click.stop.prevent="showAgreementDialog = true">《用户协议》</el-link>
              和
              <el-link type="primary" @click.stop.prevent="showPrivacyDialog = true">《隐私政策》</el-link>
            </el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleRegister" :loading="registerLoading" style="width: 100%">
              注册
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 忘记密码对话框 -->
    <el-dialog v-model="showForgotPassword" title="忘记密码" width="400px" @close="closeForgotPassword">
      <el-form :model="forgotPasswordForm" :rules="forgotPasswordRules" ref="forgotPasswordFormRef">
        <el-form-item label="手机号/邮箱" prop="contact">
          <el-input v-model="forgotPasswordForm.contact" placeholder="请输入注册时的手机号或邮箱" />
        </el-form-item>

        <el-form-item label="验证码" prop="verifyCode">
          <div class="verify-code-row">
            <el-input v-model="forgotPasswordForm.verifyCode" placeholder="请输入验证码" style="flex: 1;" />
            <el-button type="primary" @click="sendVerifyCode" :disabled="countdown > 0" :loading="sendingCode">
              {{ countdown > 0 ? `${countdown}s后重发` : '发送验证码' }}
            </el-button>
          </div>
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="forgotPasswordForm.newPassword" type="password" placeholder="请输入新密码" show-password />
        </el-form-item>

        <el-form-item label="确认新密码" prop="confirmNewPassword">
          <el-input v-model="forgotPasswordForm.confirmNewPassword" type="password" placeholder="请再次输入新密码"
            show-password />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeForgotPassword">取消</el-button>
          <el-button type="primary" @click="resetPassword" :loading="resetLoading">
            重置密码
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import {
  User,
  Lock,
  Phone,
  Message,
  OfficeBuilding
} from '@element-plus/icons-vue'
import { useAuth } from '../composables/useAuth'

const {
  loginFormRef,
  registerFormRef,
  forgotPasswordFormRef,
  showRegister,
  showForgotPassword,
  loading,
  registerLoading,
  resetLoading,
  sendingCode,
  countdown,
  loginForm,
  registerForm,
  forgotPasswordForm,
  loginRules,
  registerRules,
  forgotPasswordRules,
  handleLogin,
  handleRegister,
  resetPassword,
  refreshCaptcha,
  sendVerifyCode,
  switchToRegister,
  switchToLogin,
  openForgotPassword,
  closeForgotPassword
} = useAuth()

// 协议和隐私政策的dialog控制
const showAgreementDialog = ref(false)
const showPrivacyDialog = ref(false)
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-form {
  background: white;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  width: 100%;
  max-width: 450px;
  max-height: 90vh;
  overflow-y: auto;
}

.login-logo {
  text-align: center;
  margin-bottom: 20px;

  .logo-img {
    width: 80px;
    height: 80px;
    border-radius: 50%;
  }
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  font-size: 28px;
  color: #333;
}

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;

  h3 {
    margin: 0;
    color: #333;
  }
}

.captcha-row {
  display: flex;
  gap: 10px;
  align-items: center;
}

.verify-code-row {
  display: flex;
  gap: 10px;
  align-items: center;
}

.register-link {
  text-align: center;
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.agreement-content,
.privacy-content {
  max-height: 400px;
  overflow-y: auto;
  line-height: 1.6;

  h4 {
    color: #333;
    margin: 20px 0 10px 0;

    &:first-child {
      margin-top: 0;
    }
  }

  p {
    color: #666;
    margin-bottom: 15px;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-form {
    padding: 30px 20px;
    margin: 20px;
  }

  .login-title {
    font-size: 24px;
  }
}
</style>